<?php

namespace app\ctrl;

/**
 * 批量查域名的级别
 */
class Domainlevel extends \app\ctrl\BaseController
{

    /**
     * 显示提交域名视图
     */
    public function step1()
    {
        // 输出详情html内容
        $view = new \core\lib\View(VIEWS);
        $url = env('APP_BASEURL') . 'domainlevel/step2';
        return $view->render('domainlevel-1', [
            'v' => time(),
            'url' => $url,
        ]);
    }

    /**
     * 显示提交域名视图
     */
    public function step2()
    {
        $params = post();
        $this->validate($params, [
            'domains' => 'require',
        ]);

        // 获取最后的域名
        $temp = array_filter(explode("\n", trim($params['domains'])));
        $domains = [];
        $i = 0;
        foreach ($temp as $item) {
            $domain = trim($item);
            if (!empty($domain)) {
                $domain = strtolower(trim($domain));
                $domain = str_replace(['https://', 'http://', 'wss://', 'ftp://'], '', $domain);
                if (str_contains($domain, '/')) {
                    $domain = mb_substr($domain, 0, strpos($domain, '/'));
                }
                if (str_contains($domain, '?')) {
                    $domain = mb_substr($domain, 0, strpos($domain, '?'));
                }

                $i = $i + 1;
                $domains[] = [
                    'index' => $i,
                    'domain' => $domain,
                    'ret' => '',
                    'body' => '',
                ];

            }
        }
        if (empty($domains)) {
            error('域名参数为空', 10000);
        }
        if (count($domains) > 10000) {
            error('域名最多只能10000个', 10000);
        }

        // 输出详情html内容
        $view = new \core\lib\View(VIEWS);
        return $view->render('domainlevel-2', [
            'v' => time(),
            'url' => env('APP_BASEURL') . 'domainlevel/exec',
            'domains' => $domains,
            'domain_count' => count($domains),
            'back_url' => env('APP_BASEURL') . 'domainlevel/step1',
            'websocket_url' => env('APP_WEBSOCKET_URL', 'ws://127.0.0.1:9502'),
        ]);
    }
}